﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.DAL.Gateway
{
    public class PmGateway:DBGateway
    {
        public bool SaveProjectManager(int empId,PM pManager)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("Insert into tblProjectManager values(@empId,@projectId,@date)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@empId", SqlDbType.Int).Value = empId;
                CommandObj.Parameters.Add("@projectId", SqlDbType.Int).Value = pManager.Project.Id;
                CommandObj.Parameters.Add("@date", SqlDbType.DateTime).Value = pManager.ManagerCreateDate;
                CommandObj.ExecuteNonQuery();
                return true;

            }
            catch (Exception)
            {
                throw new Exception("Error occured while creating Project Manager, Try again");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public List<ViewProjectManager> GetProjectManagers()
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"SELECT pm.Id,proj.ProjectName,proj.startingDate,proj.ClosingDate,proj.TotalEmployee,proj.ProjectDescription, proj.Location,emp.Name,desig.Title
                                         FROM  tblProjectManager as pm INNER JOIN tblProjects as proj ON pm.ProjectId =proj.ProjectID INNER JOIN tblEmployees as emp ON pm.EmpId =emp.EmployeeId AND proj.ProjectID =emp.ProjectID INNER JOIN
                                         tblDesignations as desig ON emp.DesignationID =desig.Id";
                SqlDataReader reader = CommandObj.ExecuteReader();
                List<ViewProjectManager> viewProjectManagers=new List<ViewProjectManager>();
                while (reader.Read())
                {
                    ViewProjectManager manager=new ViewProjectManager();
                    manager.Id = Convert.ToInt32(reader[0]);
                    manager.ProjectName = reader[1].ToString();
                    manager.startingDate = Convert.ToDateTime(reader[2]);
                    manager.clossingDate = Convert.ToDateTime(reader[3]);
                    manager.totalEmployee = Convert.ToInt32(reader[4]);
                    manager.ProjectDetails = reader[5].ToString();
                    manager.Location = reader[6].ToString();
                    manager.EmployeeName = reader[7].ToString();
                    manager.Designation = reader[8].ToString();
                    viewProjectManagers.Add(manager);
                }
                return viewProjectManagers;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public string DeletePm(int id)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"Delete from tblProjectManager  where Id='" +id+ "'";
                CommandObj.ExecuteReader();
                return "Project Manager Deleted Successfully";


            }
            catch (Exception)
            {

                throw new Exception("Error occured during Deleting Project Manager");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}